
import paho.mqtt.client as mqtt

import logging
import json
import glob
import uuid
import hashlib

import logging

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger(__name__)


def send_mqtt_message(broker_address="47.96.3.242", broker_port=1883, topic="test/topic", message="test_message"):
    # 连接 MQTT 代理
    client = mqtt.Client()
    # client.connect("mqtt_broker_address", 1883)  # 替换为您的 MQTT 代理地址和端口

    # 建立连接
    client.connect(broker_address, broker_port)

    # 发布数据到指定主题
    client.publish(topic, message)

    # 断开 MQTT 连接
    client.disconnect()


def get_mac_address():
    "获取系统的MacAddress"

    if glob.glob("/root/.mac"):
        
        with open('/root/.mac') as f:

            mac = f.read().strip()
    else:    
        node = uuid.getnode()

        mac = uuid.UUID(int=node).hex[-12:]

    return mac

def mqtt_status_callback(self, status): # mqtt 回调设备新动态

        mac = get_mac_address()

        msg = {
            "mac": mac,
            "status": status,
        }

        msg = json.dumps(msg)

        send_mqtt_message(topic="rfid_cabinet/cabinet_status", message=msg)


 
def calculate_md5(filepath):
    md5 = hashlib.md5()
    
    with open(filepath, 'rb') as file:
        while True:
            data = file.read(4096) # 每次读取4KB数据
            
            if not data:
                break
                
            md5.update(data)
        
    return md5.hexdigest()

if __name__ == "__main__":

    print(get_mac_address())

    # print(calculate_md5("main.py"))